package logic;

import java.util.List;

import database.DatabaseConnector;
import database.ModelClass;
import database.User;

//		This class handles transactions between users accounts
public class TransferBTC {
	DatabaseConnector dbConnector;
	ModelClass mc;
	String transferCompleted;

	public TransferBTC(double transferAmount, String Email,
			double currentBalance, String userId, String mailTo) {
		dbConnector = new DatabaseConnector();
		mc = new ModelClass();
		System.out.println("THIS IS CURRENT BALANCE: " + currentBalance);
		System.out.println("THIS IS TRANSFER AMOUNT: " + transferAmount);
		if (currentBalance - transferAmount >= 0) {
			List<User> users = mc.getDatabaseConnector().getUsers();
			for (User user : users) {

				if (user.getEmail().equalsIgnoreCase(mailTo)) {

					double typedAmount = transferAmount;
					double destinationBalance = Double.parseDouble(dbConnector
							.getBalanceList(mailTo));
					dbConnector.updateBitcoinBalance(currentBalance
							- typedAmount, userId);
					dbConnector.updateBitcoinBalance(destinationBalance
							+ typedAmount, mailTo);

					transferCompleted = new String("TransferCompleted:YES");
					break;
				} else {
					transferCompleted = new String("TransferCompleted:NO");
				}
			}
		} else {
			transferCompleted = new String("TransferCompleted:NO");
		}

	}

	public String getIfTransferCompleted() {
		return transferCompleted;
	}
}